﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ThaireInvestmentWeb.Util;
using ThaireInvestmentWeb.WcfSerivce;

namespace ThaireInvestmentWeb.Models
{
    public class EditEquityPurchaseModel
    {
        public EquityHeader Header { get; set; }
        public EquityHeaderDTO HeaderDTO { get; set; }
        public EquityTransaction Transaction { get; set; }
        public EditEquityPurchaseModel()
        {
            Init();
        }
        void Init()
        {
            if (null == Header)
                Header = new EquityHeader();
            if (null == Transaction)
                Transaction = new EquityTransaction();
            var defaultItem = WebUtil.SelectListSelectDefault();
            TransactionTypeList = new BindingList<SelectListItem>();
            TypeOfPaymentList = new BindingList<SelectListItem> { defaultItem };
            PhysicalList = new BindingList<SelectListItem> { defaultItem };
            KeepAtList = new BindingList<SelectListItem> { defaultItem };
            BrokerList = new BindingList<SelectListItem>() { defaultItem };

        }

        public static EditEquityPurchaseModel BuildModel(EquityTransactionScreenData data)
        {
            var model = new EditEquityPurchaseModel();
            if (data.Header != null)
                model.Header = data.Header;
            if (data.Transaction != null)
            {
                model.Transaction = data.Transaction;
                model.UpdateMode = true;
            }
            else
            {
                model.SelectedTransactionType = "Purchase";
            }

            model.HeaderDTO = new EquityHeaderDTO { Header = data.Header, InvolvedPartyType = data.InvolvedType };

            //fixed value ตัวเลือก "Purchase", "Subscription"  Default = Purchase
            model.TransactionTypeList.Add(item: new SelectListItem() { Selected = false, Text = "Purchase", Value = "Purchase" });
            model.TransactionTypeList.Add(item: new SelectListItem() { Selected = false, Text = "Subscription", Value = "Subscription" });


            //fixed value  ตัวเลือก "Scrip", "scripless"
            if (data.PhysicalList != null || data.PhysicalList.Any())
            {
                foreach (var selectListItem in data.PhysicalList)
                {
                    model.PhysicalList.Add(item: new SelectListItem() { Selected = false, Text = selectListItem.ValueEN, Value = selectListItem.ID.ToString() });
                }
            }

            //fixed value  ตัวเลือก "Custodian", "OIC", "Safe"
            if (data.KeepAtList != null || data.KeepAtList.Any())
            {
                foreach (var selectListItem in data.KeepAtList)
                {
                    model.KeepAtList.Add(item: new SelectListItem() { Selected = false, Text = selectListItem.ValueEN, Value = selectListItem.ID.ToString() });
                }
            }

            if (data.PaymentTypes != null || data.PaymentTypes.Any())
            {
                foreach (var selectListItem in data.PaymentTypes)
                {
                    model.TypeOfPaymentList.Add(item: new SelectListItem() { Selected = false, Text = selectListItem.PaymentTypeName, Value = selectListItem.PaymentTypeId.ToString() });
                }
            }
            if (data.Brokers != null || data.Brokers.Any())
            {
                foreach (var selectListItem in data.Brokers)
                {
                    model.BrokerList.Add(item: new SelectListItem() { Selected = false, Text = selectListItem.BrokerNameEn, Value = selectListItem.BrokerId.ToString() });
                }
            }
            model.HeaderID = model.Header.EquityHeaderID;
            model.TransactionID = model.Transaction.EquityTransactionID;

            return model;
        }


        #region dropdownlist data
        //TransactionType
        [Required(ErrorMessage = "โปรดระบุ")]
        public string SelectedTransactionType
        {
            get { return Transaction.TransactionType; }
            set { Transaction.TransactionType = value; }
        }
        public IList<SelectListItem> TransactionTypeList { get; set; }

        //Type of Payment (.TYPEOFPAYMENT_NAME)
        [Required(ErrorMessage = "โปรดระบุ")]
        public int? SelectedTypeOfPayment
        {
            get { return Transaction.TypeofpaymentID; }
            set { Transaction.TypeofpaymentID = value; }


        }
        public IList<SelectListItem> TypeOfPaymentList { get; set; }

        //Physical
        [Required(ErrorMessage = "โปรดระบุ")]
        public string SelectedPhysical
        {
            get
            {
                if (Transaction.PhysicalID.HasValue)
                    return Transaction.PhysicalID.Value.ToString();
                return string.Empty;
            }
            set
            {
                Transaction.PhysicalID = !string.IsNullOrEmpty(value) ? int.Parse(value) : (int?)null;
            }
        }
        public IList<SelectListItem> PhysicalList { get; set; }

        //Keep at 
        [Required(ErrorMessage = "โปรดระบุ")]
        public string SelectedKeepAt
        {
            get
            {
                if (Transaction.KeepATID.HasValue)
                    return Transaction.KeepATID.Value.ToString();
                return string.Empty;
            }
            set
            {
                Transaction.KeepATID = !string.IsNullOrEmpty(value) ? int.Parse(value) : (int?)null;
            }
        }
        public IList<SelectListItem> KeepAtList { get; set; }

        public int? SelectedBroker
        {
            get { return Transaction.BrokerID; }
            set { Transaction.BrokerID = value; }
        }
        public IList<SelectListItem> BrokerList { get; set; }

        #endregion


        #region adapter data
        public int? HeaderID
        {
            get
            {
                return Transaction.EquityHeaderID;
            }

            set
            {
                Transaction.EquityHeaderID = value;
            }
        }
        public int TransactionID
        {
            get
            {
                return Transaction.EquityTransactionID;
            }

            set
            {
                Transaction.EquityTransactionID = value;
            }
        }


        //[DataType(DataType.Date)]
        //[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
        //[Display(Name = "Maturity Date")]
        //public DateTime? HeaderMaturityDate
        //{
        //    get { return Header.MaturityDate; }
        //    set { Header.MaturityDate = value; }
        //}

        [Required(ErrorMessage = "โปรดระบุ")]
        [StringLength(50, ErrorMessage=("ความยาวไม่เกิน 50 ตัวอักษร"))]
        public String RefNo
        {
            get { return Transaction.RefNO; }
            set { Transaction.RefNO = value; }
        }

        [Required(ErrorMessage = "โปรดระบุ")]
        [DataType(DataType.Date)]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
        [Display(Name = "Trade Date")]
        public DateTime? TradeDate
        {
            get { return Transaction.TradeDate; }
            set { Transaction.TradeDate = value; }
        }
        [Required(ErrorMessage = "โปรดระบุ")]
        [DataType(DataType.Date)]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
        [Display(Name = "Settlement Date")]
        public DateTime? SettlementDate
        {
            get { return Transaction.SettlementDate; }
            set { Transaction.SettlementDate = value; }
        }

        [Required(ErrorMessage = "โปรดระบุ")]
        public int? Unit
        {
            get { return Transaction.Unit; }
            set { Transaction.Unit = value; }
        }

        [Required(ErrorMessage = "โปรดระบุ")]
        public decimal? PriceUnit
        {
            get { return Transaction.PriceUnit; }
            set { Transaction.PriceUnit = value; }
        }

        [Required(ErrorMessage = "โปรดระบุ")]
        public decimal? ParUnit
        {
            get { return Transaction.ParUnit; }
            set { Transaction.ParUnit = value; }
        }

        [Required(ErrorMessage = "โปรดระบุ")]
        public decimal? ComRate
        {
            get { return Transaction.ComRate; }
            set { Transaction.ComRate = value; }
        }

        [Required(ErrorMessage = "โปรดระบุ")]
        public decimal? VatRate
        {
            get { return Transaction.VatRate; }
            set { Transaction.VatRate = value; }
        }

        [Required(ErrorMessage = "โปรดระบุ")]
        public decimal? WHTaxRate
        {
            get { return Transaction.WHTaxRate; }
            set { Transaction.WHTaxRate = value; }
        }

        #endregion


        public String SubmitBtn { get; set; }
        public String CancelBtn { get; set; }
        public bool UpdateMode { get; set; }

    }
}